﻿using QT.Lib.SixLabors.ImageSharp;
using Sunny.UI;
using System.Drawing.Text;

namespace ImageSpirit.Win
{
    public partial class FSetting : UIForm
    {
        /// <summary>
        /// 批处理参数
        /// </summary>
        public BatchParameters Parameters { get; set; } = new();

        public FSetting()
        {
            InitializeComponent();
        }

        private void FSetting_Load(object sender, EventArgs e)
        {
            InitBatchParameters();
        }

        /// <summary>
        /// 初始化批处理参数控件值
        /// </summary>
        private void InitBatchParameters()
        {
            IList<FiterKV> fiters =
            [
                new FiterKV() { Key = "灰度滤镜",Value = Filter.Grayscale },
                new FiterKV() { Key = "黑白滤镜", Value = Filter.BlackWhite },
                new FiterKV() { Key = "棕褐色滤镜", Value = Filter.Sepia },
                new FiterKV() { Key = "反转滤镜", Value = Filter.Invert },
                new FiterKV() { Key = "高斯模糊滤镜", Value = Filter.GaussianBlur },
                new FiterKV() { Key = "高斯锐化滤镜", Value = Filter.GaussianSharpen },
                new FiterKV() { Key = "高斯锐化滤镜", Value = Filter.GaussianSharpen },
                new FiterKV() { Key = "晕影滤镜", Value = Filter.Vignette },
                new FiterKV() { Key = "亮度滤镜", Value = Filter.Brightness },
                new FiterKV() { Key = "对比度滤镜", Value = Filter.Contrast },
                new FiterKV() { Key = "饱和度滤镜", Value = Filter.Saturate },
                new FiterKV() { Key = "色调滤镜", Value = Filter.Hue },
                new FiterKV() { Key = "像素化滤镜", Value = Filter.Pixelate }
            ];

            comBox_Filter.ValueMember = "Value";
            comBox_Filter.DisplayMember = "Key";
            comBox_Filter.DataSource = fiters;

            IList<StringKV> fontsKV = GetFontsKV();

            comboBox_Font.ValueMember = "Value";
            comboBox_Font.DisplayMember = "Key";
            comboBox_Font.DataSource = fontsKV;
        }

        /// <summary>
        /// 滤镜下拉列表框键值对
        /// </summary>
        private class FiterKV()
        {
            /// <summary>
            /// Key
            /// </summary>
            public string Key { get; set; }

            /// <summary>
            /// Value
            /// </summary>
            public Filter Value { get; set; }
        }

        /// <summary>
        /// 字符串下拉列表键值对
        /// </summary>
        private class StringKV()
        {
            /// <summary>
            /// Key
            /// </summary>
            public string Key { get; set; }

            /// <summary>
            /// Value
            /// </summary>
            public string Value { get; set; }
        }

        /// <summary>
        /// 获取系统已安装字体下拉键值对
        /// </summary>
        /// <returns>系统已安装字体下拉键值对</returns>
        private IList<StringKV> GetFontsKV()
        {
            // 获取系统中支持的所有字体
            var fontFamilies = SixLabors.Fonts.SystemFonts.Families;

            IList<StringKV> fontsKV = new List<StringKV>();

            // 输出所有已安装的字体名称
            foreach (var item in fontFamilies)
            {
                fontsKV.Add(new StringKV() { Key = item.Name, Value = item.Name });
            }
            return fontsKV;
        }

        /// <summary>
        /// 选择贴图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_Chartlet_ButtonClick(object sender, EventArgs e)
        {
            string file = "";
            if (FileEx.OpenDialog(ref file, "图片|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;"))
            {
                this.textBox_Chartlet.Text = file;
                Parameters.Chartlet = file;
            }
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Ok_Click(object sender, EventArgs e)
        {
            Parameters.Filter = (Filter)this.comBox_Filter.SelectedValue;
            Parameters.Amount = (float)this.doubleUpDown_Amount.Value;
            Parameters.CPoint = new SixLabors.ImageSharp.Point(this.textBox_CX.IntValue, this.textBox_CY.IntValue);
            Parameters.Opacity = (float)this.doubleUpDown_Opacity.Value;
            Parameters.Text = this.textBox_Text.Text;
            Parameters.Font = SixLabors.Fonts.SystemFonts.CreateFont(this.comboBox_Font.SelectedValue.ToString(), this.textBox_FontSize.IntValue);
            Parameters.Color = SixLabors.ImageSharp.Color.FromRgba(this.colorPicker.Value.R, this.colorPicker.Value.G, this.colorPicker.Value.B, this.colorPicker.Value.A);
            Parameters.TPoint = new SixLabors.ImageSharp.PointF(this.textBox_FX.IntValue, this.textBox_FY.IntValue);
            Parameters.IsReady = true;
            this.Close();
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_ESC_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
